package com.itheima.controller.home;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.Sms.SmsHomeRecommendProductService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.pojo.SmsHomeNewProduct;
import com.itheima.vo.pojo.SmsHomeRecommendProduct;
import com.itheima.vo.pojo.SmsHomeRecommendProduct;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/home/recommendProduct")
public class SmsHomeRecommendProductContrller {

    @DubboReference
    SmsHomeRecommendProductService recommendProductService;


    /*
     * 1.添加首页推荐
     * */
    @PostMapping("/create")
    public AjaxResult addIndex(@RequestBody List<SmsHomeRecommendProduct> SmsHomeRecommendProduct)
    {

        boolean save = recommendProductService.saveBatch(SmsHomeRecommendProduct);

        return AjaxResult.success(save);
    }


    /*
     * 2.批量删除推荐
     * */
    @PostMapping("/delete")
    public AjaxResult BatchIndexNew(@RequestParam List<Integer> ids)
    {
        boolean b = recommendProductService.removeByIds(ids);
        return AjaxResult.success(ids);

    }


    /*
    * 3.分页
    * */

    @GetMapping("/list")
    public AjaxResult AllPage(Integer pageNum , Integer pageSize , String productName , Integer recommendStatus)
    {
        LambdaQueryWrapper<SmsHomeRecommendProduct> lambdaQueryWrapper  =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ge(productName !=null,SmsHomeRecommendProduct::getProductName, productName);
        lambdaQueryWrapper.like(recommendStatus !=null ,SmsHomeRecommendProduct::getRecommendStatus, recommendStatus);
        TableDataInfo tableDataInfo  = new TableDataInfo();
        Page<SmsHomeRecommendProduct> page = new Page<>(pageNum , pageSize);
        Page<SmsHomeRecommendProduct> page1 = recommendProductService.page(page , lambdaQueryWrapper);

        tableDataInfo.setList(page1.getRecords());

        tableDataInfo.setPageNum(pageNum);

        tableDataInfo.setPageSize(pageSize);

        tableDataInfo.setTotal(page1.getTotal());
        tableDataInfo.setTotalPage(page1.getPages());

        return AjaxResult.success(tableDataInfo);
    }


    /*
     * 4.批量修改推荐状态
     * */
    @PostMapping("update/recommendStatus")
    public AjaxResult BatchAlterStatus(@RequestParam List<Integer> ids ,  Integer recommendStatus)
    {


        ArrayList<SmsHomeRecommendProduct> Product = new ArrayList<>();
        
        for (Integer id : ids) {

            SmsHomeRecommendProduct SmsHomeRecommendProduct = new SmsHomeRecommendProduct();

            SmsHomeRecommendProduct.setId(id);
            SmsHomeRecommendProduct.setRecommendStatus(recommendStatus);
            Product.add(SmsHomeRecommendProduct);
        }

        boolean b = recommendProductService.updateBatchById(Product);

        return AjaxResult.success(b);
    }


    /*
     * 5.修改推荐排序
     * */
    @PostMapping("update/sort/{id}")
    public AjaxResult AlterSort(@PathVariable Integer id , Integer sort)
    {

        SmsHomeRecommendProduct Product = new SmsHomeRecommendProduct();
        Product.setProductId(id);
        Product.setSort(sort);
        Boolean b =  recommendProductService.updateById(Product);

        return AjaxResult.success(b);
    }

}
